<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-12
 * Time: 16:34
 * 会员订单回调操作
 */

namespace app\api\service\order;


use app\api\model\MemberCost;
use app\api\model\MemberOrder;
use app\api\model\User;
use app\api\model\UserInvitation;
use app\api\model\UserInvitationMoney;
use app\api\service\Token;
use think\Db;
use think\Exception;
use think\facade\Log;

class MemberSer extends Token
{
    /**
     * 1.修改订单状态
     * 2.修改用户过期时间
     * 3.对用户上级进行返佣
     */
    // 订单信息
    private $order;


    function __construct($order)
    {
        $this->order = $order;
    }

    // 入口文件
    public function changeStatus()
    {
        if($this->order['status'] == 1) {
            Db::startTrans();
            try
            {
                $this->memberCost();
                Db::commit();
                return true;
            }
            catch (Exception $ex)
            {
                Db::rollback();
                throw $ex;
                return false;
            }
        } else {
            return true;
        }
    }


    // 会员订单返佣操作
    private function memberCost()
    {
        $invitation = UserInvitation::where('invited_person_id',$this->order['user_id'])->find();
        $memberCost = MemberCost::get($this->order['member_cost_id']);
        if($invitation) {
            if($memberCost['discount'] > 0) {
                $userInfo = User::get($invitation['inviter_id']);
                if($userInfo) {
                    $invite_money = $userInfo['invite_money'] + $memberCost['discount'];
                    $userInfo->invite_money = $invite_money;
                    $userInfo->save();
                    UserInvitationMoney::create([
                        'user_id' => $userInfo['id'],
                        'invite_id' => $this->order['user_id'],
                        'type' => 1,
                        'money' => $memberCost['discount'],
                        'user_invite_money' => $userInfo['invite_money'],
                        'order_no' => $this->order['order_no'],
                    ]);
                }
            }
        }
        // 更新用户到期时间
        $orderUser = User::get($this->order['user_id']);
        if($orderUser['member_end_time'] > time()) {
            $member_end_time = $this->changeTime($orderUser['member_end_time'],$memberCost['status']);
        } else {
            $member_end_time = $this->changeTime(strtotime(date('Y-m-d').' 23:59:59'),$memberCost['status']);
            $orderUser->member_start_time = strtotime(date('Y-m-d').' 00:00:00');
        }
        $orderUser->member_end_time = $member_end_time;
        $orderUser->save();
        // 更新订单状态
        $MemberOrder = new MemberOrder();
        $MemberOrder->save([
            'status' => 2,
            'start_time' => time(),
            'end_time' => $member_end_time
        ],['id' => $this->order['id']]);
    }

    // 会员类型转换时间
    private function changeTime($time,$memberCostStatus)
    {
        if($memberCostStatus == 1) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+1 day",$time)));
        } elseif ($memberCostStatus == 2) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+1 week",$time)));
        } elseif ($memberCostStatus == 3) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+1 month",$time)));
        } elseif ($memberCostStatus == 4) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+6 month",$time)));
        } elseif ($memberCostStatus == 5) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+1 year",$time)));
        } elseif ($memberCostStatus == 6) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+2 year",$time)));
        } elseif ($memberCostStatus == 7) {
            return strtotime(date('Y-m-d H:i:s',strtotime("+3 year",$time)));
        }
    }
}